import { addlogisticApi } from '../../api/order'
import { formatDateTime } from '../../utils/util'

Component({
  options: {
    styleIsolation: 'shared'
  },
  /**
   * 组件的属性列表
   */
  properties: {
    orderInfo: {
      type: Object,
      value: {}
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    show: false
  },

  /**
   * 组件的方法列表
   */
  methods: {
    showPopup() {
      this.setData(
        {
          show: true
        },
        () => {
          if ((this.data.orderInfo.logisticsVO ?? []).length === 0) {
            this.setData({
              'orderInfo.logisticsVO': [
                {
                  createTime: formatDateTime(new Date().getTime()),
                  logisticsName: '',
                  logisticsNum: '',
                  orderId: this.data.orderInfo.orderListVO.id,
                  state: 1,
                  type: 1
                }
              ]
            })
          }
        }
      )
    },

    onChange(e) {
      const value = e.detail
      const key = e.currentTarget.dataset.key
      const index = e.currentTarget.dataset.index
      this.setData({
        [`orderInfo.logisticsVO[${index}].${key}`]: value
      })
    },

    // async addlogistic() {
    //   const [err, data] = await addlogisticApi(this.data.orderInfo.logisticsVO)
    //   if (err) return
    // },

    /**
     * 添加单号
     */
    addExpress() {
      const list = this.data.orderInfo.logisticsVO ?? []
      const logistics = list[list.length - 1] ?? {}

      if ((logistics.logisticsName ?? '') === '' || (logistics.logisticsNum ?? '') === '') {
        wx.showToast({
          title: '请完善快递信息',
          icon: 'none'
        })
        return
      }
      list.push({
        createTime: formatDateTime(new Date().getTime()),
        logisticsName: '',
        logisticsNum: '',
        orderId: this.data.orderInfo.orderListVO.id,
        state: 1,
        type: 1
      })
      this.setData({
        'orderInfo.logisticsVO': list
      })
    },

    /**
     * 删除单号
     */
    delExpress() {
      const list = this.data.orderInfo.logisticsVO ?? []
      list.splice(list.length - 1, 1)
      this.setData({
        'orderInfo.logisticsVO': list
      })
    },

    /**
     * 提交物流
     */
    async submitLogistics() {
      const isValidate = this.data.orderInfo.logisticsVO.some(
        (item) => item.logisticsName.trim() === '' || item.logisticsNum.trim() === ''
      )
      if (isValidate) {
        wx.showToast({
          title: '请完善单号信息',
          icon: 'none'
        })
        return
      }
      const logisticsVO = JSON.parse(JSON.stringify(this.data.orderInfo.logisticsVO)).map(
        (item) => {
          delete item.id
          item.orderId = `${this.data.orderInfo.orderListVO.id}`
          return item
        }
      )

      const [logisticsErr] = await addlogisticApi({
        logisticsVO,
        orderId: this.data.orderInfo.orderListVO.id,
        type: 1
      })
      if (logisticsErr) return

      this.setData({
        show: false
      })
      this.triggerEvent('submitLogisticsSuccess', this.data.orderInfo.logisticsVO)
    },

    /**
     * 关闭弹窗
     */
    closeHandle() {
      this.setData({
        show: false
      })
    }
  }
})
